﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DrugStorehouseMIS.DAL;
using DrugStorehouseMIS.DAL.Interface;
using DrugStorehouseMIS.Comm;
using System.Data.SqlClient;

namespace DrugStorehouseMIS.BLL
{  
    public class User
    {
        #region 字段 属性

        private string mUserNum;

        public string UserNum
        {
            get { return mUserNum; }
            set { mUserNum = value; }
        }

        private string mUserType;

        public string UserType
        {
            get { return mUserType; }
            set { mUserType = value; }
        }

        private string mUserName;

        public string UserName
        {
            get { return mUserName; }
            set { mUserName = value; }
        }

        private string mUserPassword;

        public string UserPassword
        {
            get { return mUserPassword; }
            set { mUserPassword = value; }
        }

        private string mCreatedBy;

        public string CreatedBy
        {
            get { return mCreatedBy; }
            set { mCreatedBy = value; }
        }

        private DateTime mCreateDatime;

        public DateTime CreateDatime
        {
            get { return mCreateDatime; }
            set { mCreateDatime = value; }
        }

        private string mLastUpdatedBy;

        public string LastUpdatedBy
        {
            get { return mLastUpdatedBy; }
            set { mLastUpdatedBy = value; }
        }

        private DateTime mLastUpdateDatetime;

        public DateTime LastUpdateDatetime
        {
            get { return mLastUpdateDatetime; }
            set { mLastUpdateDatetime = value; }
        }

        private int mFlagDisable;

        public int FlagDisable
        {
            get { return mFlagDisable; }
            set { mFlagDisable = value; }
        }

        #endregion

        private IUserDAL userDal;

        public User()
        {
            userDal= Adapter.GetUserDAL();
        }

        //#region 用户登录
        //public static string username;
        //public static int login(string UserCode, string mUserPassword)
        //{
        //    UserDAL userDal = new UserDAL();
        //    SqlDataReader reader;
        //    reader = (SqlDataReader )userDal.Select(UserCode);
        //    if (!reader.Read ())
        //        return 0;
        //    else if (mUserPassword == reader["user_password"].ToString())
        //    {
        //        username = reader["user_name"].ToString();
        //        return 1;
        //    }
        //    else
        //        return -1;

        //}
        //#endregion

        public void FetchObject(IDataReader dr)
        {
            if (dr != null)
            {
                mUserNum = ConvertDataType.CheckFiledString(dr["user_num"]);
                mUserName = ConvertDataType.CheckFiledString(dr["user_name"]);
                mUserPassword = ConvertDataType.CheckFiledString(dr["user_password"]);
                mUserType = ConvertDataType.CheckFiledString(dr["user_type"]);
                mCreateDatime = ConvertDataType.CheckFiledDateTime(dr["created_datetime"]);
                mCreatedBy = ConvertDataType.CheckFiledString(dr["created_by"]);
                mFlagDisable = ConvertDataType.CheckFiledInt(dr["flag_disable"]);
                mLastUpdateDatetime = ConvertDataType.CheckFiledDateTime(dr["last_updated_datetime"]);
                mLastUpdatedBy = ConvertDataType.CheckFiledString(dr["last_updated_by"]);
            }
        }

        public bool CreateUser(User user)
        {
            return userDal.Insert(user.UserNum, user.UserType, user.UserName, user.UserPassword, user.CreatedBy, user.LastUpdatedBy, user.FlagDisable) > 0 ? true : false;
        }

        public bool RemoveUser()
        {
            return false;
        }

        public bool SetUser()
        {
            return false;
        }

        public int Login(string code, string pwd)
        {
            User u = new User();
            IDataReader dr = userDal.Select(code);

            if (!dr.Read())
                return 0;

            u.FetchObject(dr);

            if (u.UserPassword == pwd)
                return 1;
            else
                return -1;
        }

        public User GetUser(string code)
        {
            User obj = new User();
            IDataReader dr = userDal.Select(code);
            while (dr != null && dr.Read())
            {
                obj.FetchObject(dr);
            }
            return obj;
        }
    }
}
